package com.zxk28.sqlbuilders;

public class SQLInsertBuilder {

	private String prefixClause = "INSERT INTO ";
	private String tableClause = null;
	private String columnsClause = null;
	private String valuesClause = null;

	public SQLInsertBuilder setInsertTable(String table) {
		this.tableClause = table;

		return this;
	}

	public SQLInsertBuilder setColumns(String... columnNames) {
		this.columnsClause = " (";

		for (int i = 0; i < columnNames.length; i++) {
			this.columnsClause += columnNames[i];
			if (i < columnNames.length - 1)
				this.columnsClause += ", ";
		}
		this.columnsClause += ")";

		return this;
	}

	public SQLInsertBuilder setValues(Object... values) {
		this.valuesClause = " VALUES(";

		for (int i = 0; i < values.length; i++) {
			if (values[i] instanceof String)
				this.valuesClause += "'" + values[i].toString() + "'";
			else
				this.valuesClause += values[i].toString();

			if (i < values.length - 1)
				this.valuesClause += ", ";
		}
		this.valuesClause += ");";

		return this;
	}

	public String getCmd() {
		if (tableClause == null)
			return null;
		if (valuesClause == null)
			return null;
		if (columnsClause == null)
			return prefixClause + tableClause + valuesClause;
		else
			return prefixClause + tableClause + columnsClause + valuesClause;
	}

	@Override
	public String toString() {
		return getCmd();
	}

}
